package com.hst.web.base.proxy;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Map;

import com.hst.core.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.dao.IORDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.web.base.entity.TAccountInfo;
import com.hst.web.base.ui.EmployeeIdentify;
import com.hst.web.common.utils.ImageUtils;
import com.hst.web.sys.entity.TUser;

public class EmployeeIdentifyProxy extends DefaultDataProxy {

	@Autowired
	IORDao dao;

	@Override
	public Object save(MetaData data) throws Exception {
		EmployeeIdentify d = (EmployeeIdentify) data;
		d.setPassword(base64(d.getPassword()));
		//System.out.print("Password:" + d.getPassword() + "/");
		List<TAccountInfo> list1 = dao.list(TAccountInfo.class, new ORQuery(Op.eq, "userName", d.getUserName()));
		List<TUser> list2 = dao.list(TUser.class, new ORQuery(Op.eq, "operatorNbr", d.getUserName()));
		List<TUser> users = dao.list(TUser.class, new ORQuery(Op.eq, "employeeCode", d.getEmployeeCode()));
		if (d.getOp() == MetaData.OP_NEW) {
			if (list1.size() > 0 || list2.size() > 0) {
				if (users.size() > 0) {
					TUser user = users.get(0);
					if (d.getUserName().equals(user.getOperatorNbr()) && d.getPassword().equals(user.getPassword())) {
						TAccountInfo info = new TAccountInfo();
						info.setUserName(d.getUserName());
						info.setPassword(d.getPassword());
						info.setTelNumber(d.getTelNumber());
						dao.save(info);
					} else {
						throw new Exception("账号密码不匹配");
					}
				} else {
					throw new Exception("账户已存在");
				}
			} else {
				TAccountInfo info = new TAccountInfo();
				info.setUserName(d.getUserName());
				info.setPassword(d.getPassword());
				info.setTelNumber(d.getTelNumber());
				dao.save(info);
			}
		}
		if (d.getOp() == MetaData.OP_UPD) {
			String pwd1 = d.getPassword();
			//先解码在判断
			String pwd2 = decodeBase64(pwd1);
//			if (!checkPwd(pwd2)) {
//				throw new ServiceException("新密码需为-长度8-20位，必须包含数字，字母，特殊符号");
//			}
			if (list1.size() > 0) {
				TAccountInfo info = list1.get(0);
				info.setUserName(d.getUserName());
				info.setPassword(d.getPassword());
				info.setTelNumber(d.getTelNumber());
				dao.update(info);
			}
			//如果t_manager_info表中有关联用户,则同步修改密码
			if (users.size() > 0) {
				TUser user = users.get(0);
				user.setPassword(d.getPassword());
				dao.update(user);
			}
		}
		// 图片旋转处理
		if (!StringUtils.isEmpty(d.getDeg())) {
			ImageUtils.imgsRotate((Map<String, Integer>) JsonUtil.read(d.getDeg(), Map.class), d);
		}

		// 图片反转处理
		if (!StringUtils.isEmpty(d.getReverse())) {
			ImageUtils.imgsReverse((Map<String, Integer>) JsonUtil.read(d.getReverse(), Map.class), d);
		}
		return super.save(d);
	}

	/**
	 * 密码验证
	 * @param pwd2
	 * @return
	 */
	private static boolean checkPwd(String pwd2){
		String reg = "^(?=(.*[a-zA-Z]){1,})(?=(.*[\\d]){1,})(?=(.*(\\W|[_])){1,})(?!.*[\\s\\u4e00-\\u9fa5]).{8,20}$";
		if (pwd2.matches(reg)) {
			return true;
		} else {
			return false;
		}
	}

	private String base64(String pwd) throws UnsupportedEncodingException {
		Encoder encoder = Base64.getEncoder();
		return new String(encoder.encodeToString(pwd.getBytes("UTF-8")));
	}

	private String decodeBase64(String pwd){
		byte[] decoded = Base64.getDecoder().decode(pwd);
		return new String(decoded);
	}

}
